package com.company.action;

import org.apache.struts2.ServletActionContext;

import com.company.dao.ContactDao;
import com.company.entity.Accounts;
import com.company.entity.Contacts;
import com.company.service.AccountsService;
import com.company.service.ContactsService;

public class ContactsAction extends BaseAction {

	private Contacts contacts;
	private ContactsService contactsService;

	public Contacts getContacts() {
		return contacts;
	}

	public void setContacts(Contacts contacts) {
		this.contacts = contacts;
	}

	public ContactsService getContactsService() {
		return contactsService;
	}

	public void setContactsService(ContactsService contactsService) {
		this.contactsService = contactsService;
	}

	@Override
	public String execute() throws Exception {
		setContacts(contactsService.query());
		// TODO Auto-generated method stub
		ServletActionContext.getRequest().getSession()
				.setAttribute("name", contacts.getName());
		ServletActionContext.getRequest().getSession()
				.setAttribute("phone", contacts.getPhone());
		ServletActionContext.getRequest().getSession()
				.setAttribute("email", contacts.getEmail());
		ServletActionContext.getRequest().getSession()
				.setAttribute("address", contacts.getAddress());
		return super.execute();
	}

	public String list() throws Exception {
		return execute();
	}

	public String update() {
		if (contacts == null) {
			return ERROR;
		}
		try {
			contactsService.update(contacts);
			list();
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}
}
